/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */
const timestampToTime = (timestamp) => {
    let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s
};
/**
 * 存储localStorage
 */
const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}

/**
 * 设置cookie
 **/
function setCookie(name, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};

/**
 * 获取cookie
 **/
function getCookie(name) {
    let reg = RegExp(name + '=([^;]+)');
    let arr = document.cookie.match(reg);
    if (arr) {
        return arr[1];
    } else {
        return '';
    }
};

/**
 * 删除cookie
 **/
function delCookie(name) {
    setCookie(name, null, -1);
};

// 邮箱格式验证规则
function validateEmail(rule, value, callback) {
    const emailReg =
        /^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})$/;
    if (!value) {
        return callback(new Error("邮箱不能为空!!"));
    }
    setTimeout(() => {
        if (!emailReg.test(value)) {
            return callback(new Error("邮箱格式错误"));
        } else {
            callback();
        }
    }, 100);
};
// 手机号验证规则
function validatePhone(rule, value, callback) {
    const phoneReg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!value) {
        return callback(new Error("手机号码不能为空!!"));
    }
    setTimeout(() => {
        if (!phoneReg.test(value)) {
            return callback(new Error("手机号码格式错误"));
        } else {
            callback();
        }
    }, 100);
};
// 用户名验证规则
function validateUsername(rule, value, callback) {
    if (value.length < 2 || value.length > 6) {
        return callback(new Error("用户名不得小于6个或大于20个字符!"));
    } else {
        callback();
    }
};
// 密码格式验证规则
function validatePassword(rule, value, callback) {
    if (value.length < 6) {
        return callback(new Error("密码不能小于6位"));
    } else {
        callback();
    }
};

/**
 * 导出 
 **/
export {
    timestampToTime,
    setStore,
    getStore,
    removeStore,
    setCookie,
    getCookie,
    delCookie,
    validateEmail,
    validatePhone,
    validateUsername,
    validatePassword
}